package com.yuncheng.spcyApi.flow;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.policy.HackLoopTableRenderPolicy;
import com.yuncheng.oaApi.entity.OaFjzykEntity;
import com.yuncheng.oaApi.entity.OaJobEntity;
import com.yuncheng.oaApi.entity.OaXzswspEntity;
import com.yuncheng.oaApi.service.IOaFjzykEntityService;
import com.yuncheng.oaApi.service.IOaXzswspEntityService;
import com.yuncheng.spcyApi.config.SpcyMinioUtils;
import com.yuncheng.spcyApi.constant.common.BusinessConstant;
import com.yuncheng.spcyApi.constant.jcy.JcydjbConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.mapper.SpcyPxJbxxMapper;
import com.yuncheng.spcyApi.service.*;
import com.yuncheng.spcyApi.spcyService.SpcyJcyLxService;
import com.yuncheng.spcyApi.spcyService.SpcyProcessBusinessService;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.spcyApi.utils.WordPdfUtils;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.spcyApi.vo.jcy.JcyLxUserVo;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import com.yuncheng.spcyApi.vo.common.Result;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import utils.DateConversionUtils;
import utils.Util;
import utils.WordDocxReplaceUtils;

import javax.annotation.Resource;
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author hua
 */
@Component(value = "xzswspSpcyFlowService")
public class XzswspFlowService {
    private static final Logger log = LoggerFactory.getLogger(XzswspFlowService.class);

    @Resource
    @Lazy
    private SpcyPxJbxxMapper spcyPxJbxxMapper;

    @Resource
    @Lazy
    private ISpcyJcyPrjbxxService spcyJcyPrjbxxService;

    @Resource
    @Lazy
    private ISpcyJcyJpjbxxService spcyJcyJpjbxxService;

    @Resource
    @Lazy
    private ISpcyJcyJprymdService spcyJcyJprymdService;

    @Resource
    @Lazy
    private ISpcyJcyPrrymdService spcyJcyPrrymdService;

    @Resource
    @Lazy
    private SpcyJcyLxService spcyJcyLxService;

    @Resource
    @Lazy
    private ISpcySqsxBaiscService spcySqsxBaiscService;

    @Resource
    @Lazy
    private OaFlowService oaFlowService;

    @Resource
    @Lazy
    private IOaJobService oaJobService;

    @Resource
    @Lazy
    private IOaXzswspEntityService oaXzswspService;

    @Resource
    @Lazy
    private IOaFjzykEntityService oaFjzykService;

    @Resource
    @Lazy
    private SpcyProcessBusinessService spcyProcessBusinessService;

    @Resource
    private BusinessConstant businessConstant;

    @Resource
    private SpcyMinioUtils spcyMinioUtils;


    /**
     * 发起检查员聘任-拟聘确认名单流程
     */
    public Map initJcyPrInNpmdlc(String fprjbxxid){
        if (StringUtils.isBlank(fprjbxxid)){
            throw new BusinessException("参数为空！");
        }

        SpcyJcyPrjbxx bean = spcyJcyPrjbxxService.getById(fprjbxxid);
        if (bean == null){
            throw new BusinessException("没有相匹配的数据！");
        }

        List<SpcyJcyPrrymd> list = spcyJcyPrrymdService.list(new LambdaQueryWrapper<SpcyJcyPrrymd>()
                .eq(SpcyJcyPrrymd::getFprjbxxid, fprjbxxid));
        if (CollectionUtil.isEmpty(list)){
            throw new BusinessException("发起失败，没有对应的人员数据！");
        }

        String lcbt = "";

        lcbt += GetCurrentUserUtils.getCurrentUserName() + "提交的"+ "检查员拟聘任检查员名单";

        String lczy = "";
        lczy += "申请日期: "+ DateConversionUtils.DateToStringYYYYMMDD(new Date());
        /*lczy += "\n";
        lczy += "聘任文号: "+ Optional.ofNullable(bean.getFprwh()).orElseGet(()->"无");*/
        lczy += "\n";
        lczy += "检查类型: "+ Optional.ofNullable(bean.getFxpjcylb()).orElseGet(()->"无");
        /*lczy += "\n";
        lczy += "聘期: "+ Optional.ofNullable(bean.getFpq()).orElseGet(()->0);*/
        lczy += "\n";
        lczy += "拟聘人数: "+ list.size();

        Map resultMap = initXzswSpFlowJob(fprjbxxid, lcbt, lczy, SpcyFlowConstant.JCYPR_LCLX_NPMD);

        // oaid
        String fpid = (String) resultMap.get("fpid");
        List<OaFjzykEntity> fjList = oaFjzykService.list(new LambdaQueryWrapper<OaFjzykEntity>()
                .eq(OaFjzykEntity::getFoaid, fpid));
        if (CollectionUtil.isEmpty(fjList)) {
            initPrNpmdFile(list, fprjbxxid, fpid);
        }

        return resultMap;
    }

    /**
     * 发起检查员聘任-聘任登记确认名单流程
     */
    public Map initJcyPrInPrmdqrlc(String fprjbxxid){
        if (StringUtils.isBlank(fprjbxxid)){
            throw new BusinessException("参数为空！");
        }

        SpcyJcyPrjbxx bean = spcyJcyPrjbxxService.getById(fprjbxxid);
        if (bean == null){
            throw new BusinessException("没有相匹配的数据！");
        }

        List<SpcyJcyPrrymd> list = spcyJcyPrrymdService.list(new LambdaQueryWrapper<SpcyJcyPrrymd>()
                .eq(SpcyJcyPrrymd::getFprjbxxid, fprjbxxid));
        if (CollectionUtil.isEmpty(list)){
            throw new BusinessException("发起失败，没有对应的人员数据！");
        }

        String lcbt = "";

        lcbt += GetCurrentUserUtils.getCurrentUserName() + "提交的"+ "检查员聘任登记确认名单";

        String lczy = "";
        lczy += "申请日期: "+ DateConversionUtils.DateToStringYYYYMMDD(new Date());
        lczy += "\n";
        lczy += "聘任文号: "+ Optional.ofNullable(bean.getFprwh()).orElseGet(()->"无");
        lczy += "\n";
        lczy += "检查类型: "+ Optional.ofNullable(bean.getFxpjcylb()).orElseGet(()->"无");
        lczy += "\n";
        lczy += "聘期: "+ Optional.ofNullable(bean.getFpq()).orElseGet(()->0);
        lczy += "\n";
        lczy += "聘任人数: "+ list.size();

        Map resultMap = initXzswSpFlowJob(fprjbxxid, lcbt, lczy, SpcyFlowConstant.JCYPR_LCLX_QRMD);

        // oaid
        String fpid = (String) resultMap.get("fpid");
        List<OaFjzykEntity> fjList = oaFjzykService.list(new LambdaQueryWrapper<OaFjzykEntity>()
                .eq(OaFjzykEntity::getFoaid, fpid));
        if (CollectionUtil.isEmpty(fjList)) {
            initPrRymdQrFile(list, fprjbxxid, fpid);
        }

        return resultMap;
    }

    /**
     * 发起检查员遴选-报名审核流程
     */
    public Map initJcyLxInBmshlc(String flxjhid){
        if (StringUtils.isBlank(flxjhid)){
            throw new BusinessException("参数为空！");
        }
        SpcyPxJbxx bean = spcyPxJbxxMapper.selectById(flxjhid);
        if (bean == null){
            throw new BusinessException("没有相匹配的数据！");
        }

        /** 审核通过 */
        JcyLxUserVo shtgQuery = new JcyLxUserVo();
        shtgQuery.setFlxjhid(flxjhid);
        shtgQuery = spcyJcyLxService.ZhQueryTj(shtgQuery, 1);
        List<JcyLxUserVo> shtgList = spcyPxJbxxMapper.selectLxjhUserList(shtgQuery, new OrderItemVo());
        if (CollectionUtil.isEmpty(shtgList)){
            throw new BusinessException("发起失败，没有对应的人员数据！");
        }

        /** 审核不通过 */
        JcyLxUserVo shbtgQuery = new JcyLxUserVo();
        shbtgQuery.setFlxjhid(flxjhid);
        shbtgQuery = spcyJcyLxService.ZhQueryTj(shbtgQuery, 2);
        List<JcyLxUserVo> shbtgList = spcyPxJbxxMapper.selectLxjhUserList(shbtgQuery, new OrderItemVo());

        String lcbt = "";

//        lcbt += GetCurrentUserUtils.getCurrentUserName() + "提交的"+ "检查员遴选的报名审核";
        lcbt += "检查员遴选的报名审核";

        String lczy = "";
        /*lczy += "申请日期: "+ DateConversionUtils.DateToStringYYYYMMDD(new Date());
        lczy += "\n";
        lczy += "计划年度: "+ Optional.ofNullable(bean.getFjhnd()).orElseGet(()->"无");
        lczy += "\n";*/
        lczy += "计划名称: "+ Optional.ofNullable(bean.getFjhmc()).orElseGet(()->"无");
        lczy += "\n";
        lczy += "发文编号: "+ Optional.ofNullable(bean.getFfwbh()).orElseGet(()->"无");
        lczy += "\n";
        lczy += "检查类型: "+ Optional.ofNullable(bean.getFjhcjlb()).orElseGet(()->"无");
        lczy += "\n";
        lczy += "总报名人数: "+ (shtgList.size() + shbtgList.size());
        lczy += "\n";
        lczy += "审核通过人数: "+ shtgList.size();

        Map resultMap = initXzswSpFlowJob(flxjhid, lcbt, lczy, SpcyFlowConstant.JCYLX_LCLX_LXBMSH);

        // oaid
        String fpid = (String) resultMap.get("fpid");
        List<OaFjzykEntity> fjList = oaFjzykService.list(new LambdaQueryWrapper<OaFjzykEntity>()
                .eq(OaFjzykEntity::getFoaid, fpid));
        if (CollectionUtil.isEmpty(fjList)) {
            initJcyLxBmshFile(shtgList, flxjhid, fpid);
            initJcyLxBmshBtgFile(shbtgList, flxjhid, fpid);
        }

        return resultMap;
    }

    /**
     * 发起检查员遴选-确认名单流程
     */
    public Map initJcyLxInQrmdlc(String flxjhid){
        if (StringUtils.isBlank(flxjhid)){
            throw new BusinessException("参数为空！");
        }
        SpcyPxJbxx bean = spcyPxJbxxMapper.selectById(flxjhid);
        if (bean == null){
            throw new BusinessException("没有相匹配的数据！");
        }

        JcyLxUserVo query = new JcyLxUserVo();
        query.setFlxjhid(flxjhid);
        query = spcyJcyLxService.ZhQueryTj(query, 5);
        List<JcyLxUserVo> list = spcyPxJbxxMapper.selectLxjhUserList(query, new OrderItemVo());
        if (CollectionUtil.isEmpty(list)){
            throw new BusinessException("发起失败，没有对应的人员数据！");
        }

        String lcbt = "";

        lcbt += GetCurrentUserUtils.getCurrentUserName() + "提交的"+ "检查员遴选的确认名单";

        String lczy = "";
        lczy += "申请日期: "+ DateConversionUtils.DateToStringYYYYMMDD(new Date());
        lczy += "\n";
        lczy += "计划年度: "+ Optional.ofNullable(bean.getFjhnd()).orElseGet(()->"无");
        lczy += "\n";
        lczy += "计划名称: "+ Optional.ofNullable(bean.getFjhmc()).orElseGet(()->"无");
        lczy += "\n";
        lczy += "发文编号: "+ Optional.ofNullable(bean.getFfwbh()).orElseGet(()->"无");
        lczy += "\n";
        lczy += "检查类型: "+ Optional.ofNullable(bean.getFjhcjlb()).orElseGet(()->"无");
        lczy += "\n";
        lczy += "遴选通过人数: "+ list.size();

        Map resultMap = initXzswSpFlowJob(flxjhid, lcbt, lczy, SpcyFlowConstant.JCYLX_LCLX_LXQRMD);

        // oaid
        String fpid = (String) resultMap.get("fpid");
        List<OaFjzykEntity> fjList = oaFjzykService.list(new LambdaQueryWrapper<OaFjzykEntity>()
                .eq(OaFjzykEntity::getFoaid, fpid));
        if (CollectionUtil.isEmpty(fjList)) {
            initJcyLxQrmdFile(list, flxjhid, fpid);
        }

        return resultMap;
    }

    /**
     * 发起检查员解聘-确认名单流程
     */
    public Map initJcyJpInQrmdlc(String fjpjbxxid){
        if (StringUtils.isBlank(fjpjbxxid)){
            throw new BusinessException("参数为空！");
        }
        SpcyJcyJpjbxx bean = spcyJcyJpjbxxService.getById(fjpjbxxid);
        if (bean == null){
            throw new BusinessException("没有相匹配的数据！");
        }

        List<SpcyJcyJprymd> list = spcyJcyJprymdService.list(new LambdaQueryWrapper<SpcyJcyJprymd>()
                .eq(SpcyJcyJprymd::getFjpjbxxid, fjpjbxxid));
        if (CollectionUtil.isEmpty(list)){
            throw new BusinessException("发起失败，没有对应的人员数据！");
        }

        bean.setFjprs(list.size());
        spcyJcyJpjbxxService.updateById(bean);

        String lcbt = "";

        lcbt += GetCurrentUserUtils.getCurrentUserName() + "提交的"+ "检查员解聘确认名单";

        String lczy = "";
        lczy += "申请日期: "+ DateConversionUtils.DateToStringYYYYMMDD(new Date());
        lczy += "\n";
        lczy += "解聘文号: "+ Optional.ofNullable(bean.getFjpwh()).orElseGet(()->"无");
        lczy += "\n";
        lczy += "解聘原因: "+ Optional.ofNullable(bean.getFjply()).orElseGet(()->"无");
        lczy += "\n";
        lczy += "解聘人数: "+ Optional.ofNullable(bean.getFjprs()).orElseGet(()->0);

        Map resultMap = initXzswSpFlowJob(fjpjbxxid, lcbt, lczy, SpcyFlowConstant.JCYPR_LCLX_JPMDQR);

        // oaid
        String fpid = (String) resultMap.get("fpid");
        List<OaFjzykEntity> fjList = oaFjzykService.list(new LambdaQueryWrapper<OaFjzykEntity>()
                .eq(OaFjzykEntity::getFoaid, fpid));
        if (CollectionUtil.isEmpty(fjList)) {
            initJpFile(list, fjpjbxxid, fpid);
        }

        return resultMap;
    }

    /**
     * 发起申请事项-退回政务服务流程
     */
    public Map initSqsxThzwfwInSplc(String sqsxid){
        String lcbt = "";
        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxInfoById(sqsxid);
        if (sqsx == null){
            throw new BusinessException("没有相匹配的数据！");
        }

        String sxjssj = "";
        if (sqsx.getFslsj() != null) {
            sxjssj = DateConversionUtils.DateToYMDString(DateConversionUtils.DateToString(sqsx.getFslsj()));
        }

        lcbt += GetCurrentUserUtils.getCurrentUserName() + "提交的";
        lcbt += "关于退回"+sqsx.getFqymc()+sqsx.getFsxmc()+"申请材料的函";

        String lczy = "自治区药品监督管理局政务服务窗口：中心于"+sxjssj+"接到贵窗口转来";
        lczy += sqsx.getFqymc()+sqsx.getFsxmc()+"的申请材料。现将该企业申请材料退回。";

        Map resultMap = initXzswSpFlowJob(sqsxid, lcbt, lczy, SpcyFlowConstant.SQSX_LCLX_THZWFW);

        return resultMap;
    }

    /**
     * 验证检查员解聘-确认名单流程-是否结束
     */
    public void verifyJcyJpInQymdLcIsEnd(String fjpjbxxid){
        List<OaXzswspEntity> xzswspListByPid = getXzswspListByPid(fjpjbxxid, SpcyFlowConstant.JCYPR_LCLX_JPMDQR);
        if (CollectionUtil.isNotEmpty(xzswspListByPid)){
            throw new BusinessException("流程未审批完成，无法提交！");
        }
    }

    /**
     * 验证检查员聘任-拟聘名单流程-是否结束
     */
    public void verifyJcyPrInNpmdLcIsEnd(String fprjbxxid){
        List<OaXzswspEntity> xzswspListByPid = getXzswspListByPid(fprjbxxid, SpcyFlowConstant.JCYPR_LCLX_NPMD);
        if (CollectionUtil.isNotEmpty(xzswspListByPid)){
            throw new BusinessException("流程未审批完成，无法提交！");
        }
    }

    /**
     * 验证检查员聘任-聘任名单确认流程-是否结束
     */
    public void verifyJcyPrInPrmdQrLcIsEnd(String fprjbxxid){
        List<OaXzswspEntity> xzswspListByPid = getXzswspListByPid(fprjbxxid, SpcyFlowConstant.JCYPR_LCLX_QRMD);
        if (CollectionUtil.isNotEmpty(xzswspListByPid)){
            throw new BusinessException("流程未审批完成，无法提交！");
        }
    }

    /**
     * 验证检查员遴选-报名审核流程-是否结束
     */
    public void verifyJcyLxInBmshLcIsEnd(String flxjhid){
        List<OaXzswspEntity> xzswspListByPid = getXzswspListByPid(flxjhid, SpcyFlowConstant.JCYLX_LCLX_LXBMSH);
        if (CollectionUtil.isNotEmpty(xzswspListByPid)){
            throw new BusinessException("流程未审批完成，无法提交！");
        }
    }

    /**
     * 验证检查员遴选-确认名单流程-是否结束
     */
    public void verifyJcyLxInQrmdLcIsEnd(String flxjhid){
        List<OaXzswspEntity> xzswspListByPid = getXzswspListByPid(flxjhid, SpcyFlowConstant.JCYLX_LCLX_LXQRMD);
        if (CollectionUtil.isNotEmpty(xzswspListByPid)){
            throw new BusinessException("流程未审批完成，无法提交！");
        }
    }

    /**
     * 删除相关流程
     * @param jobid
     */
    public void deleteXzswspJobById(String jobid){
        if (StringUtils.isBlank(jobid)){
            throw new BusinessException("参数为空！");
        }

        OaJobEntity nowJob = oaJobService.getById(jobid);
        if (nowJob == null){
            throw new BusinessException("没有相匹配的数据！");
        }

        if (StringUtils.isNotBlank(nowJob.getFblzt()) && nowJob.getFblzt().equals(SpcyConstant.BLZT_YB)){
            throw new BusinessException("当前流程已经办理，无法删除！");
        }

        String oaid = nowJob.getFpid();
        if (StringUtils.isNotBlank(oaid)) {
            oaXzswspService.removeById(oaid);

            oaJobService.remove(new LambdaQueryWrapper<OaJobEntity>()
                    .eq(OaJobEntity::getFpid, oaid));

            spcyProcessBusinessService.deleteByFpid(oaid);

            List<OaFjzykEntity> fjList = oaFjzykService.list(new LambdaQueryWrapper<OaFjzykEntity>()
                    .eq(OaFjzykEntity::getFoaid, oaid));
            if (CollectionUtil.isNotEmpty(fjList)) {
                for (OaFjzykEntity fj : fjList) {
                    oaFjzykService.removeById(fj.getId());

                    spcyMinioUtils.remove(fj.getFwjlj());
                }
            }
        }
    }

    /**
     * 流程记录
     * @param fpid 业务id
     * @param ywlx 业务类型
     * */
    public List<OaJobEntity> queryOaJobList(String fpid, String ywlx){

        List<OaXzswspEntity> xzswSpList = oaXzswspService.list(new LambdaQueryWrapper<OaXzswspEntity>()
                .eq(OaXzswspEntity::getFpid, fpid)
                .eq(OaXzswspEntity::getFywlx, ywlx));

        if (CollectionUtil.isEmpty(xzswSpList)){
            return new ArrayList<>();
        }

        List<String>  idList = xzswSpList.stream().map(OaXzswspEntity::getId).collect(Collectors.toList());

        LambdaQueryWrapper<OaJobEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(OaJobEntity::getFpid, idList);
        queryWrapper.orderByAsc(OaJobEntity::getFfssj);

        return oaJobService.list(queryWrapper);
    }

    /**
     * 初始化-行政事务审批流程
     * @param pid 业务id
     * @param lcbt 流程标题
     * @param lczy 流程摘要
     * @param ywlx 业务类型
     * @return
     */
    private Map initXzswSpFlowJob(String pid, String lcbt, String lczy, String ywlx){

        String lcmbbm = SpcyFlowConstant.OA_XZSWSP_FLOWCODE;

        /*List<OaXzswsp> xzswSpList = oaXzswspService.list(new LambdaQueryWrapper<OaXzswsp>()
                .eq(OaXzswsp::getFpid, pid)
                .eq(OaXzswsp::getFywlx, ywlx)
                .eq(OaXzswsp::getFlczt, SpcyFlowConstant.OA_FLOW_LCZB));*/
        List<OaXzswspEntity> xzswSpList = getXzswspListByPid(pid, ywlx);

        String oaid = "";
        if (CollectionUtil.isEmpty(xzswSpList)) {

            OaXzswspEntity oaXzswsp = new OaXzswspEntity();
            oaid = GetUuIdUtils.ReplaceUuId();
            oaXzswsp.setId(oaid);
            CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();

            oaXzswsp.setFuserid(currentUser.getId());
            oaXzswsp.setFks(currentUser.getDeptName());
            oaXzswsp.setFnbr(currentUser.getName());
            oaXzswsp.setFrq(DateConversionUtils.DateToStringYYYYMMDD(new Date()));

            oaXzswsp.setFsx(lcbt);
            oaXzswsp.setFnbyj(lczy);

            oaXzswsp.setFnbsj(DateConversionUtils.DateToDays(new Date()));
            oaXzswsp.setFsftgdwld(SpcyFlowConstant.OA_XZSWSP_SFTGZXLDSH);
            oaXzswsp.setFlczt(SpcyFlowConstant.OA_FLOW_LCZB);
            oaXzswsp.setFlb(SpcyFlowConstant.OA_XZSWSP_LB);

            oaXzswsp.setFpid(pid);
            oaXzswsp.setFywlx(ywlx);

            Result process = oaFlowService.createProcess(lcmbbm, oaid, currentUser);
            if (process.isSuccess()){
                oaXzswspService.save(oaXzswsp);
                spcyProcessBusinessService.addOaProcess(oaid, lczy, lcbt, lcmbbm);
            }

        }else {
            oaid = xzswSpList.get(0).getId();

            spcyProcessBusinessService.addOaProcess(oaid, lczy, lcbt, lcmbbm);
        }


        /** 获取第一个环节 */
        String jobId = "";
        /*List<OaJob> jobList = oaJobService.list(new LambdaQueryWrapper<OaJob>()
                .eq(OaJob::getFpid, oaid)
                .orderByAsc(OaJob::getCreateTime));*/
        List<String> jobList = oaJobService.selectIdByFpid(oaid);
        if (CollectionUtil.isNotEmpty(jobList)){
            jobId = jobList.get(0);
        }

        String nowHjmc = "无";
        List<String> hjList = oaJobService.selectFhjmcByFpid(oaid);
        if (CollectionUtil.isNotEmpty(hjList)){
            nowHjmc = hjList.get(0);
        }

        Map resultMap = new HashMap();

        resultMap.put("jobId", jobId);
        resultMap.put("lcmbbm", lcmbbm);
        resultMap.put("fpid", oaid);

        resultMap.put("fhjmc", nowHjmc);

        return resultMap;
    }

    /**
     * 行政事务审批-业务数据列表
     * @param pid 业务id
     * @param ywlx 业务类型
     * @return
     */
    private List<OaXzswspEntity> getXzswspListByPid(String pid, String ywlx){
        return oaXzswspService.list(new LambdaQueryWrapper<OaXzswspEntity>()
                .eq(OaXzswspEntity::getFpid, pid)
                .eq(OaXzswspEntity::getFywlx, ywlx)
                .eq(OaXzswspEntity::getFlczt, SpcyFlowConstant.OA_FLOW_LCZB));
    }

    /**
     * 查询行政事务审批-详情
     * @param jobid
     * @return
     */
    public Map queryXzswspJobInfo(String jobid){
        String lcmbbm = SpcyFlowConstant.OA_XZSWSP_FLOWCODE;
        String oaid = "";

        OaJobEntity nowJob = oaJobService.getById(jobid);
        if (nowJob == null){
            throw new BusinessException("没有相匹配的数据！");
        }

        oaid = nowJob.getFpid();

        Map resultMap = new HashMap();

        resultMap.put("jobId", jobid);
        resultMap.put("lcmbbm", lcmbbm);
        resultMap.put("fpid", oaid);

        return resultMap;
    }

    /**
     * 初始化解聘文件
     * @param list 人员名单列表
     * @param fjpjbxxid 解聘id
     * @param oaid oaid (行政事务id)
      */
    private void initJpFile(List<SpcyJcyJprymd> list, String fjpjbxxid, String oaid){
        if (CollectionUtil.isEmpty(list)) {
            list = spcyJcyJprymdService.list(new LambdaQueryWrapper<SpcyJcyJprymd>()
                    .eq(SpcyJcyJprymd::getFjpjbxxid, fjpjbxxid));
        }

        String orgUrl = JcydjbConstant.JCYJP_TEMPLATE_NAME;
        String wjlx = Util.getWjHz(orgUrl); // 获取文件路径的 文件类型
        String destWordUrl = businessConstant.TEMPORARY_UPLOAD_MKDIR_ROOT + "/" + SpcyConstant.TEMP_WJJL_CHILD_URL + "/";
        String destPdfUrl = destWordUrl;
        destWordUrl += GetUuIdUtils.ReplaceUuId() + wjlx;
        destPdfUrl += GetUuIdUtils.ReplaceUuId() + ".pdf";

        orgUrl = businessConstant.UPLOAD_FILE_ROOT + "/" + JcydjbConstant.JCYJP_TEMPLATE_NAME;

        HashMap<String, Object> templateField = new HashMap<>();
        templateField.put("list", list);
        HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
        Configure config = Configure.builder().bind("list", policy).build();

        WordDocxReplaceUtils.docxConfigureReplace(orgUrl, destWordUrl, config, templateField);

        WordPdfUtils.wordToPdf(destWordUrl, destPdfUrl);

        initOaFjzyk(oaid, destPdfUrl);
    }

    /**
     * 初始化聘任-拟聘名单文件
     * @param list 人员名单列表
     * @param fprjbxxid 聘任id
     * @param oaid oaid (行政事务id)
     */
    private void initPrNpmdFile(List<SpcyJcyPrrymd> list, String fprjbxxid, String oaid){
        if (CollectionUtil.isEmpty(list)) {
            list = spcyJcyPrrymdService.list(new LambdaQueryWrapper<SpcyJcyPrrymd>()
                    .eq(SpcyJcyPrrymd::getFprjbxxid, fprjbxxid));
        }

        String orgUrl = JcydjbConstant.JCYPR_Nprymd_TEMPLATE_NAME;
        String wjlx = Util.getWjHz(orgUrl); // 获取文件路径的 文件类型
        String destWordUrl = businessConstant.TEMPORARY_UPLOAD_MKDIR_ROOT + "/" + SpcyConstant.TEMP_WJJL_CHILD_URL + "/";
        String destPdfUrl = destWordUrl;
        destWordUrl += GetUuIdUtils.ReplaceUuId() + wjlx;
        destPdfUrl += GetUuIdUtils.ReplaceUuId() + ".pdf";

        orgUrl = businessConstant.UPLOAD_FILE_ROOT + "/" + JcydjbConstant.JCYPR_Nprymd_TEMPLATE_NAME;

        HashMap<String, Object> templateField = new HashMap<>();
        templateField.put("list", list);
        HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
        Configure config = Configure.builder().bind("list", policy).build();

        WordDocxReplaceUtils.docxConfigureReplace(orgUrl, destWordUrl, config, templateField);

        WordPdfUtils.wordToPdf(destWordUrl, destPdfUrl);

        initOaFjzyk(oaid, destPdfUrl);
    }

    /**
     * 初始化聘任-名单确认文件
     * @param list 人员名单列表
     * @param fprjbxxid 聘任id
     * @param oaid oaid (行政事务id)
     */
    private void initPrRymdQrFile(List<SpcyJcyPrrymd> list, String fprjbxxid, String oaid){
        if (CollectionUtil.isEmpty(list)) {
            list = spcyJcyPrrymdService.list(new LambdaQueryWrapper<SpcyJcyPrrymd>()
                    .eq(SpcyJcyPrrymd::getFprjbxxid, fprjbxxid));
        }

        String orgUrl = JcydjbConstant.JCYPR_RymdQr_TEMPLATE_NAME;
        String wjlx = Util.getWjHz(orgUrl); // 获取文件路径的 文件类型
        String destWordUrl = businessConstant.TEMPORARY_UPLOAD_MKDIR_ROOT + "/" + SpcyConstant.TEMP_WJJL_CHILD_URL + "/";
        String destPdfUrl = destWordUrl;
        destWordUrl += GetUuIdUtils.ReplaceUuId() + wjlx;
        destPdfUrl += GetUuIdUtils.ReplaceUuId() + ".pdf";

        orgUrl = businessConstant.UPLOAD_FILE_ROOT + "/" + JcydjbConstant.JCYPR_RymdQr_TEMPLATE_NAME;

        HashMap<String, Object> templateField = new HashMap<>();
        templateField.put("list", list);
        HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
        Configure config = Configure.builder().bind("list", policy).build();

        WordDocxReplaceUtils.docxConfigureReplace(orgUrl, destWordUrl, config, templateField);

        WordPdfUtils.wordToPdf(destWordUrl, destPdfUrl);

        initOaFjzyk(oaid, destPdfUrl);
    }

    /**
     * 初始化遴选-报名审核文件 （通过）
     * @param list 人员名单列表
     * @param flxjhid 遴选id
     * @param oaid oaid (行政事务id)
     */
    private void initJcyLxBmshFile(List<JcyLxUserVo> list, String flxjhid, String oaid){
        if (CollectionUtil.isEmpty(list)) {
            JcyLxUserVo query = new JcyLxUserVo();
            query.setFlxjhid(flxjhid);
            query = spcyJcyLxService.ZhQueryTj(query, 1);
            list = spcyPxJbxxMapper.selectLxjhUserList(query, new OrderItemVo());
        }

        String orgUrl = JcydjbConstant.JCYLX_BMSH_TEMPLATE_NAME;
        String wjlx = Util.getWjHz(orgUrl); // 获取文件路径的 文件类型
        String destWordUrl = businessConstant.TEMPORARY_UPLOAD_MKDIR_ROOT + "/" + SpcyConstant.TEMP_WJJL_CHILD_URL + "/";
        String destPdfUrl = destWordUrl;
        destWordUrl += GetUuIdUtils.NotReplaceUuId() + wjlx;
        destPdfUrl += GetUuIdUtils.NotReplaceUuId() + ".pdf";

        orgUrl = businessConstant.UPLOAD_FILE_ROOT + "/" + JcydjbConstant.JCYLX_BMSH_TEMPLATE_NAME;

        HashMap<String, Object> templateField = new HashMap<>();
        templateField.put("list", list);
        HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
        Configure config = Configure.builder().bind("list", policy).build();

        WordDocxReplaceUtils.docxConfigureReplace(orgUrl, destWordUrl, config, templateField);

        WordPdfUtils.wordToPdf(destWordUrl, destPdfUrl);

        initOaFjzyk(oaid, destPdfUrl, "审核通过人员名单");
    }

    /**
     * 初始化遴选-报名审核文件 (不通过)
     * @param list 人员名单列表
     * @param flxjhid 遴选id
     * @param oaid oaid (行政事务id)
     */
    private void initJcyLxBmshBtgFile(List<JcyLxUserVo> list, String flxjhid, String oaid){
        if (CollectionUtil.isEmpty(list)) {
            return;
        }

        String orgUrl = JcydjbConstant.JCYLX_BMSH_BTG_TEMPLATE_NAME;
        String wjlx = Util.getWjHz(orgUrl); // 获取文件路径的 文件类型
        String destWordUrl = businessConstant.TEMPORARY_UPLOAD_MKDIR_ROOT + "/" + SpcyConstant.TEMP_WJJL_CHILD_URL + "/";
        String destPdfUrl = destWordUrl;
        destWordUrl += GetUuIdUtils.NotReplaceUuId() + wjlx;
        destPdfUrl += GetUuIdUtils.NotReplaceUuId() + ".pdf";

        orgUrl = businessConstant.UPLOAD_FILE_ROOT + "/" + JcydjbConstant.JCYLX_BMSH_BTG_TEMPLATE_NAME;

        HashMap<String, Object> templateField = new HashMap<>();
        templateField.put("list", list);
        HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
        Configure config = Configure.builder().bind("list", policy).build();

        WordDocxReplaceUtils.docxConfigureReplace(orgUrl, destWordUrl, config, templateField);

        WordPdfUtils.wordToPdf(destWordUrl, destPdfUrl);

        initOaFjzyk(oaid, destPdfUrl, "审核不通过人员名单");
    }

    /**
     * 初始化遴选-确认名单文件
     * @param list 人员名单列表
     * @param flxjhid 遴选id
     * @param oaid oaid (行政事务id)
     */
    private void initJcyLxQrmdFile(List<JcyLxUserVo> list, String flxjhid, String oaid){
        if (CollectionUtil.isEmpty(list)) {
            JcyLxUserVo query = new JcyLxUserVo();
            query.setFlxjhid(flxjhid);
            query = spcyJcyLxService.ZhQueryTj(query, 5);
            list = spcyPxJbxxMapper.selectLxjhUserList(query, new OrderItemVo());
        }

        String orgUrl = JcydjbConstant.JCYLX_QRMD_TEMPLATE_NAME;
        String wjlx = Util.getWjHz(orgUrl); // 获取文件路径的 文件类型
        String destWordUrl = businessConstant.TEMPORARY_UPLOAD_MKDIR_ROOT + "/" + SpcyConstant.TEMP_WJJL_CHILD_URL + "/";
        String destPdfUrl = destWordUrl;
        destWordUrl += GetUuIdUtils.ReplaceUuId() + wjlx;
        destPdfUrl += GetUuIdUtils.ReplaceUuId() + ".pdf";

        orgUrl = businessConstant.UPLOAD_FILE_ROOT + "/" + JcydjbConstant.JCYLX_QRMD_TEMPLATE_NAME;

        HashMap<String, Object> templateField = new HashMap<>();
        templateField.put("list", list);
        HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
        Configure config = Configure.builder().bind("list", policy).build();

        WordDocxReplaceUtils.docxConfigureReplace(orgUrl, destWordUrl, config, templateField);

        WordPdfUtils.wordToPdf(destWordUrl, destPdfUrl);

        initOaFjzyk(oaid, destPdfUrl);
    }

    /**
     * 初始化oa附件-并上传附件
     * @param oaid
     * @param destPdfUrl
     */
    private void initOaFjzyk(String oaid, String destPdfUrl){
        this.initOaFjzyk(oaid, destPdfUrl, "人员名单");
    }
    private void initOaFjzyk(String oaid, String destPdfUrl, String wjxsmc){

        String wjlx = ".pdf";
        String fileName = wjxsmc + wjlx;
        String wjccmc = GetUuIdUtils.ReplaceUuId() + wjlx;
        String cfwjlj = SpcyFlowConstant.OA_FILE_FJ+"/"+ DateConversionUtils.DateToYear(new Date()) +"/"+ wjccmc;
        boolean flag = spcyMinioUtils.putFlagFile("/" + cfwjlj, new File(destPdfUrl), wjlx);

        if (flag) {
            OaFjzykEntity OaFjzykEntity = new OaFjzykEntity();
            OaFjzykEntity.setFmc(fileName); //文件真实名称
            OaFjzykEntity.setFwjmc(wjccmc); //虚假名称
            OaFjzykEntity.setId(GetUuIdUtils.ReplaceUuId());
            OaFjzykEntity.setFwjlx(wjlx);
            OaFjzykEntity.setFwjlj(cfwjlj);
            OaFjzykEntity.setFscr(GetCurrentUserUtils.getCurrentUserName());
            OaFjzykEntity.setFscrid(GetCurrentUserUtils.getCurrentUserId());
            OaFjzykEntity.setFscsj(DateConversionUtils.DateToYMDHMSChineseString(new Date()));
            OaFjzykEntity.setFoaid(oaid);
            oaFjzykService.save(OaFjzykEntity);
        }
    }

    /**
     * 初始化-Oa附件文件记录
     * @param fileName
     * @param wjccmc
     * @param wjlx
     * @param cfwjlj
     * @param oaid
     */
    private void initOaFjzykByWjjl(String fileName, String wjccmc, String wjlx, String cfwjlj, String oaid){
        OaFjzykEntity OaFjzykEntity = new OaFjzykEntity();
        OaFjzykEntity.setFmc(fileName); //文件真实名称
        OaFjzykEntity.setFwjmc(wjccmc); //虚假名称
        OaFjzykEntity.setId(GetUuIdUtils.ReplaceUuId());
        OaFjzykEntity.setFwjlx(wjlx);
        OaFjzykEntity.setFwjlj(cfwjlj);
        OaFjzykEntity.setFscr(GetCurrentUserUtils.getCurrentUserName());
        OaFjzykEntity.setFscrid(GetCurrentUserUtils.getCurrentUserId());
        OaFjzykEntity.setFscsj(DateConversionUtils.DateToYMDHMSChineseString(new Date()));
        OaFjzykEntity.setFoaid(oaid);
        oaFjzykService.save(OaFjzykEntity);
    }

}
